package com.imc.platform.base.banner.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.imc.core.config.redis.RedisTimeClean;
import com.imc.platform.base.banner.dao.IBannerMapper;
import com.imc.platform.base.banner.pojo.Banner;
import com.imc.platform.base.banner.pojo.BannerItem;
import com.imc.platform.base.banner.service.IBannerService;
import com.imc.platform.base.org.dao.IOrgMapper;
import com.imc.platform.base.org.pojo.Org;
import com.imc.platform.base.org.service.IOrgService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/*
 *类描述：banner业务类
 *开发者：sjc
 *创建时间：2018-8-3 14:57:32
 */
@Service
public class BannerServiceImpl implements IBannerService, RedisTimeClean {

    @Autowired
    IBannerMapper iBannerMapper;

    @Override
    @Cacheable(value = "banner", keyGenerator = "keyGenerator")
    public PageInfo <Banner> queryBanner(int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize, true);
        PageInfo d = new PageInfo <>(iBannerMapper.queryBanner());
        return d;
    }

    /**
     * 方法描述:修改banner状态
     * SJC 2018-9-5 19:50:08
     */
    @Override
    @CacheEvict(value = "banner", allEntries = true)
    public int updateBannerState(String bnId, String state) {
        if ("false".equals(state)) {
            state = "0";
        } else if ("true".equals(state)) {
            state = "1";
        }
        return iBannerMapper.updateBannerState(bnId, state);
    }

    /**
     * 方法描述:新增banner主表
     * SJC 2018-8-28 20:28:43
     */
    @Override
    @CacheEvict(value = "banner", allEntries = true)
    public int addBanner(Banner banner) {

        int count = iBannerMapper.addBanner(banner);
        return count;

    }

    /**
     * 方法描述:修改banner名称
     * SJC 2018-8-28 20:28:43
     */
    @Override
    @CacheEvict(value = "banner", allEntries = true)
    public int updateBanner(Banner banner) {
        return iBannerMapper.updateBannerName(banner);
    }

    /**
     * 方法描述:新增banner图片
     * SJC 2018-8-28 20:58:06
     */
    @Override
    @CacheEvict(value = "banner", allEntries = true)
    public int addBannerItem(BannerItem bi) {
        int coutn = iBannerMapper.addBannerItem(bi);
        return coutn;
    }

    /**
     * 方法描述:补充banner图片内容
     * SJC 2018-8-29 17:39:32
     */
    @Override
    @CacheEvict(value = "banner", allEntries = true)
    public int updateBannerItem(BannerItem bi) {
        int count = iBannerMapper.updateBannerItem(bi);
        return count;
    }

    /**
     * 方法描述:banner图片排序
     * SJC 2018-8-29 17:39:32
     */
    @Override
    @CacheEvict(value = "banner", allEntries = true)
    public int updateBannerItemSn(List <String> bnNames) {
        List <String> ins = new ArrayList <String>();
        for (String imgName : bnNames) {
            String in = imgName.substring(imgName.lastIndexOf("/") + 1, imgName.length());
            ins.add(in);
        }
        return iBannerMapper.updateBannerItemSn(ins);

    }

    /**
     * 方法描述:删除banner图片
     * SJC 2018-8-29 17:39:32
     */
    @Override
    @CacheEvict(value = "banner", allEntries = true)
    public int delBannerItem(BannerItem bi) {
        return iBannerMapper.delBannerItem(bi);
    }

    /**
     * 方法描述:获取banner图片详情，轮播展示用
     * SJC 2018-8-30 20:18:15
     */
    @Override
    @Cacheable(value = "banner", keyGenerator = "keyGenerator")
    public List <BannerItem> getBannerItemInfo(String bnId) {
        List <BannerItem> biList = iBannerMapper.getBannerItemInfo(bnId);
        Map <String, Object> dataMap = null;
        return biList;
    }

    /**
     * 方法描述:根据id查询banner名称
     * SJC 2018-8-31 10:20:16
     */
    @Override
    public String getBannerName(String bnId) {
        String bannerName = iBannerMapper.getBannerName(bnId);
        return bannerName;
    }

    /**
     * 方法描述:根据Id删除banner
     * SJC 2018-9-4 22:11:22
     */
    @Override
    @CacheEvict(value = "banner", allEntries = true)
    public int delBanner(String bnId) {
        int count = iBannerMapper.delBanner(bnId);
        if (count != 0) {
            iBannerMapper.delBannerItems(bnId);
        }
        return count;
    }

    /**
     * 方法描述:条件查询banner详情
     * SJC 2018-9-10 10:49:53
     */
    @Override
//    @Cacheable(value = "banner", keyGenerator = "keyGenerator")
    public List <Map <String, Object>> queryBannerInfo(String bannerType) {
        return iBannerMapper.queryBannerInfo(bannerType);
    }

    @Override
    @CacheEvict(cacheNames ="banner", allEntries=true)
    public void timeClean() {

    }
}
